package util

import (
	"strings"

	"mall/common/globalkey"
	"mall/common/moneyx"
	enum "mall/service/backend/common/enum/promotion"
	"mall/service/promotion/rpc/promotionclient"
)

func ParseSorter(sorter *string) (res []*promotionclient.OrderBy) {
	if sorter == nil {
		return
	}

	sorters := strings.Split(*sorter, ";")
	if len(sorters) == 0 {
		return
	}

	for _, orderBy := range sorters {
		orderBys := strings.Split(orderBy, ":")

		if len(orderBys) == 2 {
			res = append(res, &promotionclient.OrderBy{
				Field: orderBys[0],
				Order: orderBys[1],
			})
			continue
		}

		if len(orderBys) == 1 {
			res = append(res, &promotionclient.OrderBy{
				Field: orderBys[0],
				Order: "0",
			})
		}
	}

	return
}

func ParseFilter(filter *string) (res []*promotionclient.FilterBy) {
	if filter == nil {
		return
	}

	filters := strings.Split(*filter, ";")
	if len(filters) == 0 {
		return
	}

	for _, filterBy := range filters {
		filterBys := strings.Split(filterBy, ":")

		if len(filterBys) == 2 {
			res = append(res, &promotionclient.FilterBy{
				Field: filterBys[0],
				In:    filterBys[1],
			})
		}
	}

	return
}

const (
	PromotionActivityDiscount string = "满减送"
	PromotionActivitySeckill  string = "秒杀"
	PromotionActivityPackage  string = "商品套餐"
	PromotionActivityMember   string = "会员优惠"
	PromotionActivityCharge   string = "充值优惠"
)

func GetActivityTypeText(activityType *uint32) (res string) {
	if activityType != nil {
		switch uint8(*activityType) {
		case enum.PromotionActivityDiscount:
			res = PromotionActivityDiscount
		case enum.PromotionActivitySeckill:
			res = PromotionActivitySeckill
		case enum.PromotionActivityPackage:
			res = PromotionActivityPackage
		case enum.PromotionActivityMember:
			res = PromotionActivityMember
		case enum.PromotionActivityCharge:
			res = PromotionActivityCharge
		}
	}

	return
}

const (
	PromotionMutexTypeOrder    string = "订单互斥"
	PromotionMutexTypeSpu      string = "产品互斥"
	PromotionMutexTypeSku      string = "产品规格互斥"
	PromotionMutexTypeBrand    string = "产品品牌互斥"
	PromotionMutexTypeCategory string = "产品品类互斥"
)

func GetMutexTypeText(mutexType *uint32) (res string) { // ##25.03.17##
	if mutexType != nil {
		switch uint8(*mutexType) {
		case enum.PromotionMutexTypeOrder:
			res = PromotionMutexTypeOrder
		case enum.PromotionMutexTypeSpu:
			res = PromotionMutexTypeSpu
		case enum.PromotionMutexTypeSku:
			res = PromotionMutexTypeSku
		case enum.PromotionMutexTypeBrand:
			res = PromotionMutexTypeBrand
		case enum.PromotionMutexTypeCategory:
			res = PromotionMutexTypeCategory
		default:
			res = "同类型优惠可叠加"
		}
	}

	return
}

const (
	PromotionScopeAll      string = "全场通用"
	PromotionScopeCategory string = "指定品类"
	PromotionScopeBrand    string = "指定品牌"
	PromotionScopeProduct  string = "指定产品"
)

func GetScopeTypeText(scopeType *uint32) (res string) { // ##25.03.17##
	if scopeType != nil {
		switch uint8(*scopeType) {
		case enum.PromotionScopeProduct:
			res = PromotionScopeProduct
		case enum.PromotionScopeBrand:
			res = PromotionScopeBrand
		case enum.PromotionScopeCategory:
			res = PromotionScopeCategory
		case enum.PromotionScopeAll:
			res = PromotionScopeAll
		}
	}

	return
}

const (
	PromotionActivityRuleGroupAmount string = "满N元可用,还差M元"
	PromotionActivityRuleGroupNumber string = "满N件可用,还差M件"
)

func GetGroupModeText(groupMode *uint32) (res string) {
	if groupMode != nil {
		switch uint8(*groupMode) {
		case enum.PromotionActivityRuleGroupAmount:
			res = PromotionActivityRuleGroupAmount
		case enum.PromotionActivityRuleGroupNumber:
			res = PromotionActivityRuleGroupNumber
		}
	}

	return
}

const (
	PromotionActivityRuleGroupOne  string = "阶梯满减"
	PromotionActivityRuleGroupMany string = "循环满减"
)

func GetGroupTypeText(groupType *uint32) (res string) {
	if groupType != nil {
		switch uint8(*groupType) {
		case enum.PromotionActivityRuleGroupOne:
			res = PromotionActivityRuleGroupOne
		case enum.PromotionActivityRuleGroupMany:
			res = PromotionActivityRuleGroupMany
		}
	}

	return
}

const (
	PromotionActivityRuleTypeDiscount     string = "订单金额优惠"
	PromotionActivityRuleTypeFreeShipping string = "包邮"
	PromotionActivityRuleTypeExtraPoint   string = "赠额外积分"
	PromotionActivityRuleTypeCoupon       string = "赠优惠券"
)

func GetRuleTypeText(ruleType *uint32) (res string) {
	if ruleType != nil {
		switch uint8(*ruleType) {
		case enum.PromotionActivityRuleTypeDiscount:
			res = PromotionActivityRuleTypeDiscount
		case enum.PromotionActivityRuleTypeFreeShipping:
			res = PromotionActivityRuleTypeFreeShipping
		case enum.PromotionActivityRuleTypeExtraPoint:
			res = PromotionActivityRuleTypeExtraPoint
		case enum.PromotionActivityRuleTypeCoupon:
			res = PromotionActivityRuleTypeCoupon
		}
	}

	return
}

const (
	PromotionActivityRuleModeAmount   string = "满减"
	PromotionActivityRuleModeDiscount string = "满折"
	PromotionActivityRuleModeFixed    string = "限价"
)

func GetRuleModeText(ruleType *uint32, ruleMode *uint32) (res string) {
	if ruleType != nil && ruleMode != nil {
		switch uint8(*ruleType) {
		case enum.PromotionActivityRuleTypeDiscount:
			switch uint8(*ruleMode) {
			case enum.PromotionActivityRuleModeAmount:
				res = PromotionActivityRuleModeAmount
			case enum.PromotionActivityRuleModeDiscount:
				res = PromotionActivityRuleModeDiscount
			case enum.PromotionActivityRuleModeFixed:
				res = PromotionActivityRuleModeFixed
			}
		case enum.PromotionActivityRuleTypeFreeShipping:
			switch uint8(*ruleMode) {
			case enum.PromotionActivityRuleModeAmount:
				res = "整单包邮"
			case enum.PromotionActivityRuleModeDiscount:
				res = "仅活动商品包邮"
			}
		}
	}

	return
}

const (
	PromotionCouponTypeReceive  string = "可领取"
	PromotionCouponTypeExchange string = "可兑换"
	PromotionCouponTypePresent  string = "可赠送"
)

func GetCouponTypeText(couponType *uint32) (res string) { // ##25.03.17##
	if couponType != nil {
		switch uint8(*couponType) {
		case enum.PromotionCouponTypeReceive:
			res = PromotionCouponTypeReceive
		case enum.PromotionCouponTypeExchange:
			res = PromotionCouponTypeExchange
		case enum.PromotionCouponTypePresent:
			res = PromotionCouponTypePresent
		}
	}

	return
}

const (
	PromotionCouponModeAmount string = "满N元可用,还差M元"
	PromotionCouponModeNumber string = "满N件可用,还差M件"
)

func GetCouponModeText(couponMode *uint32) (res string) { // ##25.03.17##
	if couponMode != nil {
		switch uint8(*couponMode) {
		case enum.PromotionCouponModeAmount:
			res = PromotionCouponModeAmount
		case enum.PromotionCouponModeNumber:
			res = PromotionCouponModeNumber
		}
	}

	return
}

const (
	PromotionCouponRuleAmount              string = "减N元"
	PromotionCouponRuleDiscount            string = "打N折"
	PromotionCouponRuleDeduction           string = "抵扣N元"
	PromotionCouponRuleOrderFreeShipping   string = "整单包邮"
	PromotionCouponRuleProductFreeShipping string = "仅活动商品包邮"
	PromotionCouponRuleFixedPrice          string = "N元"
)

func GetDiscountModeText(discountMode *uint32) (res string) { // ##25.03.17##
	if discountMode != nil {
		switch uint8(*discountMode) {
		case enum.PromotionCouponRuleAmount:
			res = PromotionCouponRuleAmount
		case enum.PromotionCouponRuleDiscount:
			res = PromotionCouponRuleDiscount
		case enum.PromotionCouponRuleDeduction:
			res = PromotionCouponRuleDeduction
		case enum.PromotionCouponRuleOrderFreeShipping:
			res = PromotionCouponRuleOrderFreeShipping
		case enum.PromotionCouponRuleProductFreeShipping:
			res = PromotionCouponRuleProductFreeShipping
		case enum.PromotionCouponRuleFixedPrice:
			res = PromotionCouponRuleFixedPrice
		}
	}

	return
}

func GetDiscountValueText(discountValue string) (res string, err error) { // ##25.03.17## 打几折
	return moneyx.FormatDiscountValue(discountValue)
}

func GetDiscountValue(discountMode *uint32, discountValue *string) (res string) { // ##25.03.22##
	if discountMode != nil && discountValue != nil {
		switch uint8(*discountMode) {
		case enum.PromotionCouponRuleAmount: // 减N元
			// if amount, err := decimal.NewFromString(*discountValue); err == nil {
			// 	res = amount.Mul(decimal.NewFromInt(int64(globalkey.GemRatio))).Truncate(2).String()
			// }
			res = *discountValue
		case enum.PromotionCouponRuleDiscount: // 打N折
			if amount, err := moneyx.FormatDiscountValue(*discountValue); err == nil {
				res = amount + "折券"
			} else {
				// logx.Errorw("GetDiscountValue err", logx.Field("detail", err.Error()))
			}
		}
	}

	return
}

const (
	PromotionCouponExpireStatic  string = "固定有效期"
	PromotionCouponExpireDynamic string = "动态有效期"
)

func GetExpireModeText(couponMode *uint32) (res string) {
	if couponMode != nil {
		switch uint8(*couponMode) {
		case enum.PromotionCouponExpireStatic:
			res = PromotionCouponExpireStatic
		case enum.PromotionCouponExpireDynamic:
			res = PromotionCouponExpireDynamic
		}
	}

	return
}

const (
	PromotionCouponStatusNotStarted string = "未生效"
	PromotionCouponStatusStarted    string = "待使用"
	PromotionCouponStatusFinished   string = "已过期"
	PromotionCouponStatusInvalid    string = "已失效"
	PromotionCouponStatusUsed       string = "已使用"
)

func GetCouponStatusText(status *uint32) (res string) {
	if status != nil {
		switch int32(*status) {
		case globalkey.StatusNotStarted:
			res = PromotionCouponStatusNotStarted
		case globalkey.StatusStarted:
			res = PromotionCouponStatusStarted
		case globalkey.StatusFinished:
			res = PromotionCouponStatusFinished
		case globalkey.StatusInvalid:
			res = PromotionCouponStatusInvalid
		case globalkey.StatusUsed:
			res = PromotionCouponStatusUsed
		}
	}

	return
}
